//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;
using Style = CommunityServer.Controls.Style;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.PhotoAdmin
{
	/// <summary>
	/// Summary description for IFrameHost.
	/// </summary>
	public class IFrameHost : BaseGalleryAdminPage
	{

		#region Child Controls

		protected SectionEditControl SectionEdit;
		protected GroupEditControl GroupEdit;
		protected Style Style1;
		protected Style Style2;
		protected Style Style3;
		protected Script Script1;
		protected Script Script2;
		protected Head Head1;
		protected ResourceLinkButton Save;
		protected CommunityServer.Controls.MPContent bcr;
		protected StatusMessage Status;

		#endregion

		private void Page_Load(object sender, EventArgs e)
		{

			Status.Visible = false;

			CSContext cntx = CSContext.Current;
			// ID Notes:  -1 = add, 0 = do not display, >0 = edit
				int groupID = cntx.GetIntFromQueryString("GroupID", 0);
				int sectionID = cntx.GetIntFromQueryString("SectionID", 0);

				// Determine whether to display the SectionEdit control or GroupEdit control
				if (groupID > 0 && sectionID != 0)
				{
					SectionEdit.Visible = true;
					SectionEdit.SectionID = sectionID;
					SectionEdit.GroupID = groupID;
					if(sectionID != -1)
						SectionEdit.Gallery = CommunityServer.Galleries.Galleries.GetGallery(sectionID);
					else
						SectionEdit.Gallery = new Gallery();
					Save.Visible = true;
				}
				else if (groupID > 0)
				{
					GroupEdit.Visible = true;
					GroupEdit.ApplicationType = ApplicationType.Gallery;
					GroupEdit.GroupID = groupID;
					Save.Visible = true;
				}
				else
				{
					GroupEdit.Visible = false;
					Save.Visible = false;
				}
			if ( !Page.IsPostBack ) 
			{
				// Display a message if a Group or Section was just added/deleted
				if (cntx.GetIntFromQueryString("SectionDeleted", -1) > 0)
					DisplayStatusMessage(true, "CP_Forums_SectionEdit_StatusDeleteSuccess");

			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			Save.Click += new EventHandler(Save_Click);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void DisplayStatusMessage(bool success, string resourceName)
		{
			Status.Success = success;
			Status.Visible = true;
			Status.ResourceName = resourceName;
		}

		private void Save_Click(object sender, EventArgs e)
		{
			bool redirect = false;
			string script = @"
					<script language=""javascript"">
						{0};
					</script>";
			if(!Page.IsValid)
				return;

			if(SectionEdit.Visible)
			{
				redirect = SectionEdit.Save();
				string command = string.Format("window.top.updateSectionNode('{0}', '{1}')", SectionEdit.Gallery.Name, SectionEdit.Gallery.IsActive); 
				script = string.Format(script, command); 

			}
			if(GroupEdit.Visible)
			{
				redirect = GroupEdit.Save();
			
				string command = string.Format("window.top.updateGroupNode('{0}')", GroupEdit.CurrentGroupName); 
				script = string.Format(script, command); 
			}
			if(redirect)
			{
				DisplayStatusMessage(true, "CP_SaveSuccessful");

				Page.RegisterStartupScript("Redirect",script) ;
			}

		}
	}
}
